﻿using System;
using System.Linq;
using System.Web.Mvc;
using Diploma.Models;
using Diploma.Validation;

namespace DiplomaWeb.Controllers
{
    [Authorize(Roles = "Administrator, SystemManager")]
    public class StudentsGroupsController : MasterController
    {
        public StudentsGroupsController(DataManager dataManager)
            : base(dataManager)
        {
        }

        public ActionResult Level(int id)
        {
            QualificationLevel level = DataManager.QualificationLevelsRepository.GetLevel(id);
            if (level == null)
                throw new Exception();
            ViewData["DefaultAction"] = "Delete";
            return PartialView("~/Views/StudentsGroups/ViewGroupListControl.ascx",
                               DataManager.QualificationLevelsRepository.GetStudentGroupsGrouped(level));
        }

        public ActionResult LevelJSON(int id)
        {
            QualificationLevel level = DataManager.QualificationLevelsRepository.GetLevel(id);
            if (level == null)
                throw new Exception();

            return Json(from g in DataManager.QualificationLevelsRepository.GetStudentGroups(level)
                        select new {id = g.StudentsGroupId, name = g.Name}, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Add()
        {
            ViewData["Levels"] = new SelectList(DataManager.QualificationLevelsRepository.GetAllLevels(),
                                                "QualificationLevelId", "");

            return PartialView("~/Views/Administrators/Groups/AddGroupControl.ascx");
        }

        [HttpPost]
        public ActionResult Add(StudentsGroup group)
        {
            ValidationErrors = Validator.ValidateStudentsGroup(group);
            if (ValidationErrors.Count() == 0)
            {
                DataManager.StudentsGroupsRepository.Add(group);
            }

            return Json(ValidationErrors, "text/plain", JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            StudentsGroup group = DataManager.StudentsGroupsRepository.GetStudentsGroup(id);
            if (group == null)
                throw new Exception();
            ViewData.Model = group;
            ViewData["Levels"] = new SelectList(DataManager.QualificationLevelsRepository.GetAllLevels(),
                                                "QualificationLevelId", "Name", group.QualificationLevel);

            return PartialView("~/Views/Administrators/Groups/EditGroupControl.ascx");
        }

        [HttpPost]
        public ActionResult Edit(StudentsGroup group)
        {
            ValidationErrors = Validator.ValidateStudentsGroup(group);
            if (ValidationErrors.Count() == 0)
            {
                DataManager.StudentsGroupsRepository.Edit(group);
            }

            return Json(ValidationErrors, "text/plain", JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Delete(Guid[] selectedGroups)
        {
            foreach (Guid groupId in selectedGroups)
            {
                StudentsGroup group = DataManager.StudentsGroupsRepository.GetStudentsGroup(groupId);
                if (group == null)
                    throw new Exception();
                DataManager.StudentsGroupsRepository.Delete(group);
            }
            return new EmptyResult();
        }

        [HttpPost]
        public ActionResult Restore(Guid[] selectedGroups)
        {
            foreach (Guid groupId in selectedGroups)
            {
                DataManager.StudentsGroupsRepository.Restore(groupId);
            }
            return new EmptyResult();
        }

        public ActionResult GetDeleted()
        {
            ViewData.Model = DataManager.StudentsGroupsRepository.GetDeletedGroupsGroupesByLevel();
            ViewData["DefaultAction"] = "Restore";
            return PartialView("~/Views/StudentsGroups/ViewGroupListControl.ascx");
        }
    }
}